package com.linkstec.bookshop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.linkstec.bookshop.pojo.Order;
import com.linkstec.bookshop.pojo.User;
import com.linkstec.bookshop.service.CartService;
import com.linkstec.bookshop.service.OrderService;
import com.linkstec.bookshop.util.Page;
import com.linkstec.bookshop.util.PageBean;
import com.linkstec.bookshop.vo.ScartVo;

/**
 * 
 * @ClassName:  OrderController   订单controll
 * @Description:TODO   
 * @author:  JGQiang
 * @date:   2018年11月27日 上午10:45:58
 */
@Controller
@RequestMapping("order")
public class OrderController {

	@Autowired
	private CartService cartService;

	@Autowired
	private OrderService orderService;

	/**
	 * 
	 * @Description:TODO初次生成订单
	 * @author: JGQiang
	 * @date:   2018年11月27日 上午10:46:13    
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "toOrder")
	public ModelAndView toOrder(HttpServletRequest req) {
		ModelAndView mv = new ModelAndView();
		User user = (User) req.getSession().getAttribute("loginUser");
		List<ScartVo> cartList = (List<ScartVo>) req.getSession().getAttribute("cartList");
		int oId = 0;
		try {
			oId = orderService.toOrder(user, cartList);// 初次生成订单
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 根据订单id查看订单信息
		Order order = new Order();
		List<ScartVo> scartVosList = null;
		try {
			order = orderService.findOneOrder(oId);
			scartVosList = orderService.findOrderDetail(oId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		mv.addObject("order", order);
		mv.addObject("scartVosList", scartVosList);
		mv.setViewName("order");
		return mv;
	}

	/**
	 * 
	 * @Description:TODO 直接支付订单
	 * @author: JGQiang
	 * @date:   2018年11月27日 上午10:46:30    
	 * @param order
	 * @return
	 */
	@RequestMapping("payOrder")
	public String payOrder(Order order) {
		ModelAndView mv = new ModelAndView();
		try {
			orderService.payOrder(order);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/order/tomyOrder?page=1";
	}
	
	/**
	 * 
	 * @Description:TODO 查看我的订单
	 * @author: JGQiang
	 * @date:   2018年11月27日 上午11:16:20    
	 * @param req
	 * @param page
	 * @return
	 */
	@RequestMapping(value="tomyOrder")
	public ModelAndView tomyOrder(HttpServletRequest req,int page){
		ModelAndView mv = new ModelAndView();
		User user =(User) req.getSession().getAttribute("loginUser");
			PageBean<Order> pageBean = new PageBean<Order>();
		try {
			pageBean = orderService.findOrderByUidAndPage(page,user.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("pageBean", pageBean);
		mv.setViewName("orderList");
		return mv;
	}
	
	/**
	 *  
	 * @Description:TODO 未支付订单进行 支付
	 * @author: JGQiang
	 * @date:   2018年11月27日 下午3:03:31    
	 * @param oId
	 * @return
	 */
	@RequestMapping(value ="topayOrder")
	public ModelAndView topayOrder(int oId){
		ModelAndView mv= new ModelAndView();
		Order order = new Order();
		List<ScartVo> scartVosList = null;
		try {
			order = orderService.findOneOrder(oId);
			scartVosList = orderService.findOrderDetail(oId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		mv.addObject("order", order);
		mv.addObject("scartVosList", scartVosList);
		mv.setViewName("order");
		return mv;
	}
	
	@RequestMapping(value="updateState")
	public String updateState(int oState,int oId){
		
		try {
			orderService.updateState(oState,oId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "redirect:/order/tomyOrder?page=1";
	}
}
